#include "MainWindow.h"
#include "resource.h"

void RepeaterWindow::OnHotKey(int nHotKeyID, UINT uModifiers, UINT uVirtKey)
{
    PlayerState ps = player_.GetState();
    if(uVirtKey == VK_F3){
        ps.startPos = player_.GetState().curentPos ;
        player_.SetState(ps);
    }
    if(uVirtKey == VK_F4){
        ps.endPos = player_.GetState().curentPos;
        ps.currentState = PlayerState::repeate;
        player_.SetState(ps);
        player_.Repeate();
    }

    if(uVirtKey == VK_F2){
        player_.PlayFrom(ps.endPos); 
        ps.startPos = ps.endPos;
        ps.endPos = 0;
        ps.currentState = PlayerState::play;
        player_.SetState(ps);

    }
    if(uVirtKey == VK_F5){
        ps.currentState = PlayerState::play;
        bar_.InvalidateRect(NULL, TRUE);
        player_.PlayFrom(0);
        player_.SetState(ps);
    }
}

LRESULT CALLBACK RepeaterWindow::OnNotifyMode(UINT msg, WPARAM wParam, LPARAM lParam, BOOL& handled)
{
    long mode = (long)lParam;
    if(mode == MCI_MODE_STOP){
        if( player_.GetState().currentState == PlayerState::repeate){
            bar_.InvalidateRect(NULL, TRUE);
            player_.Repeate();
        }
        else{
            bar_.InvalidateRect(NULL, TRUE);
            player_.PlayFrom(0);
        }
    }
    return true;

}
void RepeaterWindow::OnTimer(UINT_PTR nIDEvent){
    bar_.Update(player_.GetState());
}
LRESULT CALLBACK RepeaterWindow::OnStep(UINT msg, WPARAM wParam, LPARAM lParam, BOOL& handled)
{
    bar_.Update(player_.GetState());
    return TRUE;
}

RepeaterWindow::RepeaterWindow(): rect_(0, 0, 550, 50), 
                                barRect_(2, 27, 546, 46), 
                                titleBarRect_(0, 0, 550, 27), 
                                CloseRect_(520, 2, 547, 20)
{
    Create(NULL, rect_, TEXT("MainWindow"));
    int width = ::GetSystemMetrics(SM_CXFULLSCREEN);
    SetWindowPos(HWND_TOPMOST, width / 4, 100, 0, 0, SWP_NOSIZE);

    //closeButton_.Create(m_hWnd, CloseRect_, NULL, WS_CHILD|BS_OWNERDRAW, 0, IDB_CLOSE);
    //closeButton_.ShowWindow(SW_SHOW);

    bar_.Create(*this, barRect_, TEXT("PROGRESS_BAR"));
    DragAcceptFiles(TRUE);
    player_.Init(m_hWnd, NULL); 

    hotkey_ = GlobalAddAtom(TEXT("ZHUBICEN_REPEATER"));
    RegisterHotKey(m_hWnd, hotkey_, 0, VK_F1);
    RegisterHotKey(m_hWnd, hotkey_, 0, VK_F2);
    RegisterHotKey(m_hWnd, hotkey_, 0, VK_F3);
    RegisterHotKey(m_hWnd, hotkey_, 0, VK_F4);
    RegisterHotKey(m_hWnd, hotkey_, 0, VK_F5);

}
void RepeaterWindow::OnDropFiles(HDROP hDropInfo)
{
    TCHAR fileName[256];
    ::DragQueryFile(hDropInfo, 0, fileName, 256);
    bar_.InvalidateRect(NULL, TRUE);
    player_.Play(fileName);
}
void RepeaterWindow::OnPaint(CDCHandle dc)
{
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(&ps);
    Graphics gfx(hdc);

    Gdiplus::Rect rect(rect_.left, rect_.top, rect_.right, rect_.bottom);
    LinearGradientBrush br2(	rect,	Color(42, 43, 44),	 Color(15, 15, 15), LinearGradientModeVertical  );
    Gdiplus::Status st2 = gfx.FillRectangle(&br2,rect); 
    
    //Gdiplus::Rect titleBarRect(titleBarRect_.left, titleBarRect_.top, titleBarRect_.right, titleBarRect_.bottom);
    //LinearGradientBrush br(	titleBarRect,	Color(216, 237, 255),	 Color(163, 216, 254), LinearGradientModeVertical  );
    //Gdiplus::Status st = gfx.FillRectangle(&br,titleBarRect);

    EndPaint(&ps);

}
void RepeaterWindow::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    if(nIDCtl == IDB_CLOSE){
        HDC hdc = lpDrawItemStruct->hDC;
        RECT rect = lpDrawItemStruct->rcItem;

        Rect rc(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
        Graphics gfx(hdc);
        SolidBrush blackBrush(Color(0, 0, 0));
        gfx.FillRectangle(&blackBrush, rc);

        if(lpDrawItemStruct->itemState &  ODS_SELECTED
            || lpDrawItemStruct->itemState & ODS_FOCUS){
            rc.Inflate(-1, -1);
            SolidBrush redBrush(Color(228, 96, 96));
            gfx.FillRectangle(&redBrush, rc);

            rc.Inflate(-1, -1);
            LinearGradientBrush graRedBrush(rc, Color(214, 41, 41), Color(132, 19, 19), LinearGradientModeVertical);
            gfx.FillRectangle(&graRedBrush, rc);
        }


    }

}
UINT RepeaterWindow::OnNcHitTest(CPoint point)
{
    return HTCAPTION;
}
LRESULT RepeaterWindow::OnClose(){
    DestroyWindow();
    return 0;
}
LRESULT RepeaterWindow::OnCreate(LPCREATESTRUCT lpcs){

    SetTimer(timerId_, 500);
    return 0;
}
LRESULT RepeaterWindow::OnDestroy(){
    PostQuitMessage(0);
    return 0;
}   
